<template>
	<view class="my">
    <view class="myHeaderBg"></view>
		<view class="my-avatar">
      <u-avatar :text="avatarText" bg-color="#fffcf3" size="180"></u-avatar>
			<view class="my-text">{{ username }}</view>
		</view>
		<view class="my-content">
			<view class="text-items">
				<infoItem :itemText="'我的消息'" :iconSrc="'../../../static/myMsg.png'" :myMsg="true"></infoItem>
				<infoItem :itemText="'我的订单'" :iconSrc="'../../../static/order.png'" :myOrder="true"></infoItem>
				<infoItem :itemText="'意见反馈'" :iconSrc="'../../../static/feedback.png'" :feedBack="true"></infoItem>
				<infoItem :infoText="440551" :itemText="'联系客服'" :iconSrc="'../../../static/customer-phone.png'" :callPhoneNumber="true"></infoItem>
        <u-button type="primary" plain @click="unlogin" size="medium" style="margin-top: 15%;">退出登录</u-button>
      </view>
		</view>
    <u-tabbar v-model="current" :list="list" :mid-button="true" @change="pageChange" active-color="#007AFF"></u-tabbar>
    <u-no-network></u-no-network>
	</view>
</template>

<script>
	import infoItem from '../../../components/personal/infoItem'
  import { cabinetControl, getRelay } from "@/common/js/api/api.js";
	export default {
		onLoad() {
      uni.startPullDownRefresh();
		},
    onPullDownRefresh() {
      this.username = uni.getStorageSync('username');
      this.avatarText = (this.username.slice(0,1)).toUpperCase();
      setTimeout(() => {
        uni.stopPullDownRefresh();
      }, 500);
    },
		data() {
			return{
        username: '',
        avatarText: '',
				list: [{
				    iconPath: "home",
				    selectedIconPath: "home-fill",
				    text: '首页',
				    customIcon: false,
				  },
				  {
				    iconPath: "https://cdn.uviewui.com/uview/common/min_button_select.png",
				    selectedIconPath: "https://cdn.uviewui.com/uview/common/min_button_select.png",
				    text: '扫码充电',
				    midButton: true,
				    customIcon: false,
				  },
				  {
				    iconPath: "account",
				    selectedIconPath: "account-fill",
				    text: '我的',
				    customIcon: false,
				  },
				],
				current: 2
			}
		},
		components: {
			infoItem
		},
		methods: {
			unlogin() {
        uni.showModal({
          title: '提示',
          content: '确认退出登录吗？',
          success: (res) => {
            if (res.confirm) {
              this.username = '';
              this.avatarText = '';
              uni.clearStorageSync('user');
              uni.clearStorageSync('token');
              uni.clearStorageSync('username');
              this.$forceUpdate();
              uni.navigateTo({
                url: '../../login/login'
              })
            }
          }
        });
      },
      pageChange(index) {
        if(index == 0) {
          uni.switchTab({
            url: '../home/home'
          })
          uni.startPullDownRefresh();
          this.$bus.$emit('pageIndex',index);
        }
        if(index == 1) {
          uni.scanCode({
            onlyFromCamera: true,
            success: (res) => {
              // var data = eval("[" + res.result + "]");
              // var openData = {};
              // data.forEach(item => {
              //   openData = item;
              // })
              var openData = eval("[" + res.result + "]")[0];
              openData.open = true;
              uni.showModal({
                title: '提示',
                // content: '是否要打开' + (parseInt(openData.smart.addr) + 1) + '号柜？',
                content: '是否要打开该充电仓？',
                success: (res) => {
                  if (res.confirm) {
                    this.$bus.$emit('pageIndex',2);
                    cabinetControl(openData).then((res) => {
                      uni.navigateTo({
                        url: '../../lockOpen/lockOpen'
                      })
                    }).catch((err) => {
                      uni.showToast({
                        title: err.response.data.message,
                        icon: "error",
                        duration: 2000
                      });
                    });
                  } else if (res.cancel) {
                    this.$bus.$emit('pageIndex',2);
                  }
                }
              });
            }
          });
        }
      },
      beforeDestroy() {
        this.$bus.$off('pageIndex') //解绑navMenu事件
      }
		},
		mounted() {
			this.$bus.$on('pageIndex', (data)=>{
			  this.current = data;
			})
		}
	}
</script>

<style>
	@import url("../../../common/css/my.css");
</style>
